package com.zhongfl.crm.lixiao.batch.service.lx2crm;

import com.heptagram.lixiao.bean.response.opportunity.LXOpportunityResponse;
import com.zhongfl.crm.model.LxCustomerOrder;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;

/**
 * 客户订单(励销机会) 同步服务 抽象类
 *
 * @author wang.yuchuan
 * @date 2018/9/17
 */
public abstract class AbstractCustomerOrderSyncService extends AbstractLx2CRMService {
    public static final String DATE_PATTERN = "yyyy-MM-dd HH:mm";

    public LxCustomerOrder buildCustomerOrder(LXOpportunityResponse.DataResult opportunity) throws ParseException {
        LxCustomerOrder customerOrder = new LxCustomerOrder();

        customerOrder.setLxId(opportunity.getId());
        customerOrder.setCreatedAt(DateUtils.parseDate(opportunity.getCreatedAt(), DATE_PATTERN));
        customerOrder.setUpdatedAt(DateUtils.parseDate(opportunity.getUpdatedAt(), DATE_PATTERN));
        customerOrder.setUserId(opportunity.getUser().getId());
        customerOrder.setUserName(opportunity.getUser().getName());
        customerOrder.setGetTime(opportunity.getGetTime());
        customerOrder.setCreatorId(opportunity.getCreator().getId());
        customerOrder.setCreatorName(opportunity.getCreator().getName());
        customerOrder.setOwnedDepartmentId(opportunity.getOwnedDepartment().getId());
        customerOrder.setOwnedDepartmentName(opportunity.getOwnedDepartment().getName());
        customerOrder.setJfDepartment(opportunity.getTextAssetF35226());
        customerOrder.setTotalRate(opportunity.getNumericAsset7de9b6());
        customerOrder.setChannelRate(opportunity.getNumericAsset86eeee());
        customerOrder.setLoanDuration(opportunity.getNumericAssetF59409().intValue());
        customerOrder.setTitle(opportunity.getTitle());
        customerOrder.setCustomerId(opportunity.getCustomer().getId());
        customerOrder.setCustomerName(opportunity.getCustomer().getName());
        customerOrder.setStage(opportunity.getStage());
        customerOrder.setStageMapped(opportunity.getStageMapped());
        customerOrder.setExpectAmount(opportunity.getExpectAmount());
        customerOrder.setExpectSignDate(opportunity.getExpectSignDate());
        customerOrder.setGetTime(opportunity.getGetTime());

        customerOrder.setLstJfAmibaIds(opportunity.getTextAsset6517e5());
        customerOrder.setJfAmibaNames(opportunity.getTextAsset6517e5Display());
        if (!CollectionUtils.isEmpty(customerOrder.getLstJfAmibaIds())) {
            customerOrder.setJfAmibaIds(StringUtils.join(customerOrder.getLstJfAmibaIds(), ";"));
        }
        customerOrder.setLxOrderNo(opportunity.getTextAsset2b129b());

        return customerOrder;
    }
}
